<?php
	use\ampleharvest\code\cConfig;
	require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/config/cConfig.php';
	$codeConfig = new \ampleharvest\code\cConfig();
	
	require_once(dirname(dirname(dirname(__FILE__)))) . '/Tables/cTABLE_Data.php';
	require_once(dirname(dirname(dirname(__FILE__)))) . '/cAmpleHarvestLog.php';
	require_once 'cDoc.php';

	require_once $codeConfig->C_STR_SOLR_LIB_PATH . "/Service-RAB.php";
	class cSolr extends Apache_Solr_Service_RAB
	{
		const C_STR_FORMAT_DATE = "Y-m-d\TH:i:s\Z";

		const C_STR_KEY_INDEX_DATE = "index_date";
		const C_STR_KEY_TITLE = "title";

		const C_STR_PARAM_FACET = "facet";
		const C_STR_PARAM_FACET_DATE = "facet.date";
		const C_STR_PARAM_FACET_DATE_GAP = "facet.date.gap";
		const C_STR_PARAM_FACET_DATE_START = "facet.date.start";
		const C_STR_PARAM_FACET_DATE_END = "facet.date.end";
		const C_STR_PARAM_FACET_FIELD = "facet.field";
		const C_STR_PARAM_FACET_LIMIT = "facet.limit";
		const C_STR_PARAM_FACET_MIN_COUNT = "facet.mincount";
		const C_STR_PARAM_HIGHLIGHT = "hl";
		const C_STR_PARAM_HIGHLIGHT_FIELDS = "hl.fl";
		const C_STR_PARAM_HIGHLIGHT_REQUIRE_MATCH = "hl.requireFieldMatch";
		const C_STR_PARAM_RETURN_FIELDS = "fl";
		const C_STR_PARAM_RETURN_SORT = "sort";

		protected $m_obj_TABLE_Data = null;
		protected $m_obj_Log = null;
		protected $m_bool_Rebuild = false;

		protected $m_arr_DB_Update = array();
	
		function __construct($host, $port, $path)
		{			  
		    //Work around for old constructor
			if ($host == null)
			{
			    $host = cConfig::C_STR_SOLR_HOST;
				$port = cConfig::C_INT_SOLR_PORT;
				$path = cConfig::C_STR_SOLR_SVR_PATH;
			}
		
		
			$this->m_obj_Log = new cAmpleHarvestLog();
			$this->m_obj_TABLE_Data = new cTABLE_Data();
			
			$this->m_obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_DATA_INDEX_INFO,
				"Connecting to Solr Server ",
				cAmpleHarvestLog::C_INT_LEVEL_DEBUG);
			$this->m_obj_Log->do_Log(cAmpleHarvestLog::c_str_ACTION_VALUE,
				"Host - " . $host,
				cAmpleHarvestLog::C_INT_LEVEL_DEBUG);
			$this->m_obj_Log->do_Log(cAmpleHarvestLog::c_str_ACTION_VALUE,
				"Port - " . $port,
				cAmpleHarvestLog::C_INT_LEVEL_DEBUG);
			$this->m_obj_Log->do_Log(cAmpleHarvestLog::c_str_ACTION_VALUE,
				"Path - " . $path,
				cAmpleHarvestLog::C_INT_LEVEL_DEBUG);

			parent::__construct($host, $port, $path);

			if (!$this->ping())
			{
				$this->m_obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_DATA_INDEX_ERROR,
					"Solr Server is not responding",
					cAmpleHarvestLog::C_INT_LEVEL_ERRORS);
				throw new Exception("Solr Server (".$host.":".$port."/".$path.") is not responding");
			}
		}

		public static function load_AHDoc($obj_Doc)
		{
			set_time_limit(30);

			$obj_SolrDoc = new cDoc();

			if (!$obj_SolrDoc->load_FromJson($obj_Doc))
			{
				throw new Exception("Failed to create Solr Document from Json");
			}

			return $obj_SolrDoc->get_Solr_Document();
		}

		protected function update_StatusSuccess()
		{
			foreach ($this->m_arr_DB_Update as $arr_Update)
			{
				$this->m_obj_TABLE_Data->clear();
				$this->m_obj_TABLE_Data->load_DataFromID($arr_Update["id"]);
				$this->m_obj_TABLE_Data->set_data();
				$this->m_obj_TABLE_Data->set_status(cTABLE_Data::C_INT_STATUS_INDEX_SUCCESS);
				$this->m_obj_TABLE_Data->set_index_token($arr_Update["token"]);
				$this->m_obj_TABLE_Data->set_index_date(date("Y-m-d H:i:s"));

				if (!$this->m_obj_TABLE_Data->save_Data2DB())
				{
					$this->m_obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_DATA_INDEX_ERROR,
						"Failed to update Data record to reflect the Error Status id = " . $arr_Update["id"],
						cAmpleHarvestLog::C_INT_LEVEL_ERRORS);
					print("Failed to update data Status.\n");
				}

			}
			$this->m_arr_DB_Update = array();
		}

		protected function update_StatusFail()
		{
			foreach ($this->m_arr_DB_Update as $arr_Update)
			{
				$this->m_obj_TABLE_Data->clear();
				$this->m_obj_TABLE_Data->load_DataFromID($arr_Update["id"]);
				$this->m_obj_TABLE_Data->set_data();
				$this->m_obj_TABLE_Data->set_status(cTABLE_Data::C_INT_STATUS_INDEX_FAIL);

				if (!$this->m_obj_TABLE_Data->save_Data2DB())
				{
					$this->m_obj_Log->do_Log(cAmpleHarvestLog::C_STR_ACTION_DATA_INDEX_ERROR,
						"Failed to update Data record to reflect the Error Status id = " . $arr_Update["id"],
						cAmpleHarvestLog::C_INT_LEVEL_ERRORS);
					print("Failed to update data Status.\n");
				}
			}
			$this->m_arr_DB_Update = array();
		}

		protected function load_Pages2Update($int_Chunk)
		{
			//Search for all Rows that have a current status of New
			$this->m_obj_TABLE_Data->start_Group();

			$this->m_obj_TABLE_Data->add_ColumnQuery($this->m_obj_TABLE_Data->m_int_COL_Status, cTABLE_Data::C_INT_STATUS_STAGE_1_SUCCESS);

			$this->m_obj_TABLE_Data->end_Group();
			/*
			 $this->m_obj_TABLE_Data->start_Group(cQuery::c_str_JOIN_OR);
									
			 $this->m_obj_TABLE_Data->add_ColumnQuery($this->m_obj_TABLE_Data->m_int_COL_ProcessToken,
			 $this->m_obj_TABLE_Data->get_Column_Name($this->m_obj_TABLE_Data->m_int_COL_IndexToken),
			 cCondition::c_str_OPERATOR_NOT,
			 cQuery::c_str_JOIN_AND,
			 cCondition::C_INT_FORMAT_COLUMN);
									
			 $this->m_obj_TABLE_Data->add_ColumnQuery($this->m_obj_TABLE_Data->m_int_COL_Status,
			 cTABLE_Data::C_INT_STATUS_INDEX_SUCCESS);
									
			 $this->m_obj_TABLE_Data->end_Group();
			 */
			$this->m_obj_TABLE_Data->set_Limit($int_Chunk);

			return $this->m_obj_TABLE_Data->do_Search();
		}
	}
?>